package com.itmd.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

@Configuration

@ComponentScan("com.itmd")
@PropertySource("classpath:jdbc.properites")
public class DataSourceConfiguration {

    @Value("${jdbc.Driver}")
    private String driver;


    @Value("${jdbc.jdbcUrl}")
    private String jdbcUrl;

    @Value("${jdbc.user}")
    private String user;

    @Value("${jdbc.password}")
    private  String password;

    @Bean("DataSource")   //spring会将当前方法的返回值以指定的名称存储到spring 容器中
    public DataSource getDataSource() throws SQLException, PropertyVetoException {
         DataSource dataSource = new ComboPooledDataSource();
//         Connection connection =  dataSource.getConnection();
         ((ComboPooledDataSource) dataSource).setDriverClass(driver);
         ((ComboPooledDataSource) dataSource).setJdbcUrl(jdbcUrl);
         ((ComboPooledDataSource) dataSource).setUser(user);
         ((ComboPooledDataSource) dataSource).setPassword(password);
         return dataSource;
  }

}
